package com.company.service;

import com.company.dao.AccountDao;
import com.company.pojo.Account;

import java.math.BigDecimal;
import java.sql.SQLException;
import java.util.List;

/**
 * 账户的业务层实现类
 */
public class AccountService {
    private AccountDao accountDao;

    /**
     * 转账
     *
     * @param sourceName
     * @param targetName
     * @param money
     */
    public void transfer(String sourceName, String targetName, BigDecimal money) {
//        try {
//            TransactionManager.beginTransaction();
            // 根据名称查询两个账户信息
            Account source = accountDao.findByName(sourceName);
            Account target = accountDao.findByName(targetName);
            // 转出账户减钱，转入账户加钱
            source.setMoney(source.getMoney().subtract(money));
            target.setMoney(target.getMoney().subtract(money));
            // 更新两个账户
            accountDao.update(source);
            int i = 1 / 0;                  //模拟转账异常
            accountDao.update(target);

//            TransactionManager.commit();
//        } catch (Exception e) {
//            TransactionManager.rollback();
//            e.printStackTrace();
//        } finally {
//            TransactionManager.release();
//        }

    }

    /**
     * 开户 - 保存账户
     *
     * @param account
     * @throws SQLException
     */
    public void saveAccount(Account account) throws SQLException {

//        try {
////            TransactionManager.beginTransaction();
//            TransactionManager.beginTransaction();
            accountDao.save(account);
//            TransactionManager.commit();
//            TransactionManager.commit();
//        } catch (Exception e) {
////            TransactionManager.rollback();
//            TransactionManager.rollback();
//            e.printStackTrace();
//        } finally {
////            TransactionManager.release();
//            TransactionManager.release();
//        }
    }

    /**
     * 更新账户
     *
     * @param account
     * @throws SQLException
     */
    public void updateAccount(Account account) throws SQLException {
//        try {
//            TransactionManager.beginTransaction();
            accountDao.update(account);
//            TransactionManager.commit();
//        } catch (Exception e) {
//            TransactionManager.rollback();
//            e.printStackTrace();
//        } finally {
//            TransactionManager.release();
//        }
    }

    /**
     * 销户 - 删除账户
     *
     * @param accountId
     * @throws SQLException
     */
    public void deleteAccount(String accountId) throws SQLException {
//        try {
//            TransactionManager.beginTransaction();
            accountDao.delete(accountId);
//            TransactionManager.commit();
//        } catch (Exception e) {
//            TransactionManager.rollback();
//            e.printStackTrace();
//        } finally {
//            TransactionManager.release();
//        }
    }

    /**
     * 通过 “账号” 查找用户信息
     *
     * @param accountId
     * @return
     * @throws SQLException
     */
    public Account findAccountById(String accountId) throws SQLException {
        Account account = null;
//        try {
//            TransactionManager.beginTransaction();
            account = accountDao.findById(accountId);
//            TransactionManager.commit();
//            return account;
//        } catch (Exception e) {
//            TransactionManager.rollback();
//            e.printStackTrace();
//        } finally {
//            TransactionManager.release();
//        }
        return null;
    }

    /**
     * 查找所有账户
     *
     * @return
     * @throws SQLException
     */
    public List<Account> findAllAccount() throws SQLException {
        List<Account> accounts = null;
//        try {
//            TransactionManager.beginTransaction();
            accounts = accountDao.findAll();
//            TransactionManager.commit();
//            return accounts;
//        } catch (Exception e) {
//            TransactionManager.rollback();
//            e.printStackTrace();
//        } finally {
//            TransactionManager.release();
//        }
        return null;
    }

    public void setAccountDao(AccountDao accountDao) {
        this.accountDao = accountDao;
    }
}
